package com.voodoo.lib_http.request;

import com.voodoo.lib_http.utils.Exceptions;

import java.util.Map;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Author: voodoo
 * CreateDate: 2021/05/29
 * Description:
 */
public class PostStringRequest extends OkHttpRequest{

    public static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain; charset=utf-8");
    public static MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

    String content;
    MediaType mediaType;

    public PostStringRequest(String url, Object tag, Map<String, Object> params, Map<String, String> headers, String content, MediaType mediaType, int id) {
        super(url, tag, params, headers, id);

        this.content = content;
        this.mediaType = mediaType;

        if (this.content == null) {
            Exceptions.illegalArgument("你到是添加点内容啊（content），传String你不加内容，玩呢？");
        }
        if (this.mediaType == null) {
            this.mediaType = MEDIA_TYPE_JSON;
        }
    }

    @Override
    protected RequestBody buildRequestBody() {
        return RequestBody.create(mediaType, content);
    }

    @Override
    protected Request buildRequest(RequestBody requestBody) {
        return builder.post(requestBody).build();
    }

}
